package wowraidmanager.client.wishlist.top;

import java.util.ArrayList;

import wowraidmanager.client.service.wishlist.MemberService;
import wowraidmanager.client.service.wishlist.MemberServiceAsync;
import wowraidmanager.client.service.wishlist.RaidbossDroplistService;
import wowraidmanager.client.service.wishlist.RaidbossDroplistServiceAsync;
import wowraidmanager.client.wishlist.utils.RaidbossDroplistUtil;
import wowraidmanager.client.wishlist.wishlist.EditwishlistGameVersion;
import wowraidmanager.shared.model.wishlist.Member;
import wowraidmanager.shared.model.wishlist.RaidbossDroplist;
import wowraidmanager.shared.model.wishlist.UserStatus;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.Widget;

public class TopGameVersion extends Composite{

    private static GuildWishlistUiBinder uiBinder = GWT.create(GuildWishlistUiBinder.class);

    protected RaidbossDroplistServiceAsync raidbossDroplistService = GWT.create(RaidbossDroplistService.class);

    protected MemberServiceAsync memberService = GWT.create(MemberService.class);

    UserStatus userStatus;

    ArrayList<RaidbossDroplist> raodbossDroplist;

    ArrayList<Member> guildMainMember;

    ArrayList<Member> guildSubMember;

    @UiField
    TabPanel gameVersionTab;

    @UiField
    Label title;

    @UiField
    Label wishlist;

    @UiField
    Button editwishlist;

    @UiField
    ListBox userTypeListBox;

    interface GuildWishlistUiBinder extends UiBinder<Widget, TopGameVersion>{
    }

    public TopGameVersion(UserStatus userStatus){
        initWidget(uiBinder.createAndBindUi(this));
        this.userStatus = userStatus;
        setTitle();
        getRaidbossdroplist();
        setButton();
        setListBox();
    }

    private void setListBox(){
        userTypeListBox.addChangeHandler(new ChangeHandler(){

            public void onChange(ChangeEvent event){
                getuserTypeListBox();
            }
        });
    }

    private void setButton(){
        editwishlist.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                setEditwishlist();

            }
        });
    }

    protected void setEditwishlist(){
        FlowPanel mainpanel = (FlowPanel)getParent();
        mainpanel.add(new EditwishlistGameVersion(userStatus));
        this.removeFromParent();

    }

    private void getRaidbossdroplist(){
        raidbossDroplistService.getRaidbossDroplist(new AsyncCallback<ArrayList<RaidbossDroplist>>(){

            public void onSuccess(ArrayList<RaidbossDroplist> result){
                raodbossDroplist = result;
                getguildMember();
            }

            public void onFailure(Throwable caught){
                // TODO 自動生成されたメソッド・スタブ

            }
        });

    }

    protected void getguildMember(){
        memberService.getAllMember(userStatus.getMember(), new AsyncCallback<ArrayList<Member>>(){

            public void onSuccess(ArrayList<Member> result){
                setGuildMember(result);
            }

            public void onFailure(Throwable caught){
                // TODO 自動生成されたメソッド・スタブ

            }
        });
    }

    protected void setGuildMember(ArrayList<Member> result){
        guildMainMember = new ArrayList<Member>();
        guildSubMember = new ArrayList<Member>();
        for(Member member:result){
            if(member.getMembertype().equals("Officer") || member.getMembertype().equals("Member")){
                guildMainMember.add(member);
            }else{
                guildSubMember.add(member);
            }
        }
        getuserTypeListBox();
    }

    private void getuserTypeListBox(){
        if(userTypeListBox.getItemText(userTypeListBox.getSelectedIndex()).equals("MainCharacter")){
            setGameversionTab(guildMainMember);
        }else{
            setGameversionTab(guildSubMember);
        }
    }

    protected void setGameversionTab(ArrayList<Member> guildMember){
        gameVersionTab.clear();
        ArrayList<String> gameVersionList = RaidbossDroplistUtil.getGameversion(raodbossDroplist);

        for(String gameVersion:gameVersionList){
            ArrayList<RaidbossDroplist> newRaidbossdroplist = RaidbossDroplistUtil.getRaidbossdroplistGameVersion(raodbossDroplist, gameVersion);
            gameVersionTab.add(new TopRaidzone(gameVersion, userStatus, newRaidbossdroplist, guildMember), gameVersion);
        }
        gameVersionTab.selectTab(0);
    }

    private void setTitle(){
        if(userStatus.getMember() != null){
        title.setText("Welcome, " + userStatus.getMember().getMemberName() + "!");
        wishlist.setText(userStatus.getMember().getGuild() + "/"  + userStatus.getMember().getRealm() + " Wishlist");
        }else{
            title.setText("Admin Mode");
        }
    }

}
